<script>
var app = getApp();

export default {
  name: "testResults",
  data() {
    return {
      id: '',
      testResults: {},
      avatarUrl: '',
      current: null,
    }
  },
  onLoad(options) {
    uni.setNavigationBarColor({
      backgroundColor: '#c496e8',
      frontColor: '#ffffff'
    });
    uni.setBackgroundColor({
      backgroundColorTop: '#c496e8'
    });
    if (options.id) {
      this.id = options.id;
      this.init()
    }
  },
  created() {

  },
  methods: {
    init() {
      const that = this;
      const a = uni.getStorageSync('COOK_U')
      var o = {
        submitok: 'answer',
        openid: a.openid,
        id: that.id,
      };
      uni.showLoading({
        title: '玩命加载中',
        mask: true
      });
      app.globalData.ajax({
        url: 'emotion.php',
        data: o,
        success: function (e) {
          console.log('获取资料', e)
          uni.hideLoading();
          if ('1' === e.flag) {
            that.setData({
              testResults: e,
            });
            that.findRangeAdvanced(+that.testResults.score, that.testResults.subjsonstr);
          } else {
            uni.showToast({
              title: e.msg,
              icon: 'none',
              mask: true
            });
          }
        }
      });
    },
    findRangeAdvanced(value, ranges) {
      // 参数验证
      if (typeof value !== 'number' || !Array.isArray(ranges)) {
        console.log('参数类型错误：value必须是数字，ranges必须是数组');
      }

      // 确保数值是整数
      const intValue = Math.floor(value);
      for (let i = 0; i < ranges.length; i++) {
        const range = ranges[i];
        // 转换边界值为数字
        const lowerBound = parseInt(range.s1);
        const upperBound = parseInt(range.s2);
        // 检查边界值是否有效
        if (isNaN(lowerBound) || isNaN(upperBound)) {
          console.warn(`区间 ${i} 的边界值无效：s1=${range.s1}, s2=${range.s2}`);
        }
        if (intValue >= lowerBound && intValue <= upperBound) {
          console.log('index', i)
          console.log('matchedValue', intValue)
          this.current = i
          return
        }
      }
    }
  }
}
</script>

<template>
  <view class="testResults">
    <view class="rTop"></view>
    <view class="backgroundBox">
      <view class="uinfo">
        <image :src="testResults.path_s_url" class="recommendedImg"
               mode="aspectFill"></image>
      </view>
      <view class="score">
        <p class="fraction">{{ testResults.score }}分</p>
        <p class=" text">
          {{ testResults.answer }}
        </p>
        <view class="tubiao">
          <view class="tubiaoList">
            <view v-for="(item,itemIndex) in testResults.subjsonstr" :key="itemIndex" class="tubiaoListItem">
              <text v-if="current === itemIndex" class="alyIcon-daosanjiao tubiaoItem"></text>
              <view v-if="item.s1 === item.s2" class="number">{{ item.s1 }}分</view>
              <view v-else class="number">{{ item.s1 }}-{{ item.s2 }}分</view>
              <view :style="'height:' + (60 +itemIndex * 110) + 'rpx;opacity:'+ (0.2+itemIndex*0.1)  "
                    class="li"></view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="interpretation">
      <view class="hText">综合解读</view>
      <view class="interpretationBox">
        <view class="Subheading">● 评估标准</view>
        <p>{{ testResults.pgbz }}</p>
      </view>
      <view class="interpretationBox">
        <view class="Subheading">● 评估依据</view>
        <p>{{ testResults.pgyj }}</p>
      </view>
    </view>


  </view>
</template>

<style lang="scss" scoped>
@import "testResults.scss";
</style>